package com.koron.css2.newProjectCenter.impl;

import com.koron.css2.ServerInterface;
import com.koron.css2.baseConfig.mapper.PubReceiptMapper;
import com.koron.css2.newProjectCenter.bean.PjInfoBean;
import com.koron.css2.newProjectCenter.bean.PjTerminationBean;
import com.koron.css2.newProjectCenter.bean.query.PjTerminationQueryBean;
import com.koron.css2.newProjectCenter.mapper.PjInfoMapper;
import com.koron.css2.newProjectCenter.mapper.PjTerminationMapper;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.utils.JsonUtils;
import com.koron.util.Constant;
import com.koron.util.DBSourceUtils;
import com.koron.util.Tools;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

import java.util.Map;

/**
 * 工程终止查询详情
 * 1. 前端传query
 * 2. 数据处理
 * 3. 返回数据
 */
public class PjTerminationQuery implements ServerInterface {

    // 工程报装单据类型前缀
    private static final String PJ_TYPE = "pj_info_new#";

    @Override
    public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {

        try {
            PjTerminationQueryBean pjTerminationQueryBean = JsonUtils.objectToPojo(req.getData(), PjTerminationQueryBean.class);
            PjTerminationMapper pjTerminationMapper = factory.getMapper(PjTerminationMapper.class);
            PjInfoMapper pjInfoMapper = factory.getMapper(PjInfoMapper.class);
            PubReceiptMapper receiptMapper = factory.getMapper(PubReceiptMapper.class, DBSourceUtils.getDbEnv(userInfo.getCurWaterCode()));
            Map<String, String> TERMINATION_REASON = Tools.mapDicByCode(factory, "PROJECT_TERMINATION_REASON");
            if (StringUtils.isEmpty(pjTerminationQueryBean.getId())) {
                return MessageBean.create(Constant.MESSAGE_INT_FAIL, "id 不能为空", Void.class);
            }
            // 查询工程终止单据信息
            PjTerminationBean pjTerminationBean = pjTerminationMapper.selectById(pjTerminationQueryBean.getId());
            // 查询工程报装主单据信息
            PjInfoBean pjInfo = pjInfoMapper.selectById(pjTerminationBean.getPjId());
            // 通过查询流程节点信息表获取到流程终止节点的中文名称
            String nodeCodeName = receiptMapper.getProcessNodeName(PJ_TYPE + pjInfo.getPjType(), pjTerminationBean.getNodeCode());
            // 获取到流程终止节点的中文名称
            if (StringUtils.isEmpty(nodeCodeName)) {
                return MessageBean.create(Constant.MESSAGE_INT_FAIL, "工程终止详情查询失败，错误的终止节点", null);
            }
            pjTerminationBean.setNodeCode(nodeCodeName);
            pjTerminationBean.setPjProcessInstanceId(pjInfo.getProcessInstanceId());
            pjTerminationBean.setTerminationReasonName(TERMINATION_REASON.get(pjTerminationBean.getTerminationReason()));
            MessageBean<PjTerminationBean> messageBean = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "工程终止详情查询成功", PjTerminationBean.class);
            messageBean.setData(pjTerminationBean);
            return messageBean;
        } catch (Exception e) {
            logger.error("工程终止详情查询失败", e);
            return MessageBean.create(Constant.MESSAGE_INT_FAIL, "工程终止详情查询失败", null);
        }
    }
}
